package eventos.barraMenu;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;

import vista.paneles.hojaDibujo.OrientacionHoja;
import vista.ventanas.VentanaEditor;

public class EventoImprimir extends MouseAdapter 
{
	private VentanaEditor ventana;
	
	public EventoImprimir (VentanaEditor ventana)
	{
		super();
		this.ventana= ventana;
	}
	
	public void mouseClicked(MouseEvent e)
	{
		PrinterJob job = PrinterJob.getPrinterJob();
		PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
		attr_set.add(MediaSizeName.ISO_A4);
		attr_set.add(new Copies(1));
		
		if (this.ventana.getHojaDeDibujo().getOrientacion() == OrientacionHoja.SENTIDO_HORIZONTAL)
			attr_set.add(OrientationRequested.LANDSCAPE);
		 
        job.setPrintable(this.ventana);
        /* locate a print service that can handle the request */
        PrintService[] services = 	PrinterJob.lookupPrintServices();

        if (services.length > 0) {
                try {
                	job.setPrintService(services[0]);
                	job.pageDialog(attr_set);
                        if(job.printDialog(attr_set)) {
                        	job.print(attr_set);
                        }
                } catch (PrinterException pe) { 
        		 Logger.getLogger(ventana.getClass().getName()).log(Level.SEVERE, null, pe);
        	 }
        }
	}	
}
